<?php
class RM_User_PickupDropoffController extends RM_Controller
{
    public function summaryAction()
    {

    }

    public function getlocationsJsonAction(){
        $dao = new RM_PickupDropoff();

        $total = $dao->getAll()->count();
        $rows = $dao->getAll()->toArray();

        $data = array();
        foreach($rows as $row){
            $data[] = array("id"=>$row['id'],'name'=>$row['name']);
        }

        return array(
            'data' => $data
        );
    }

    public function listlocationsAction(){

        ob_clean();
        $unitID = $this->_getParam('unit_id');

        $model = new RM_PickupDropoff();
        $locations = $model->getByUnitID($unitID);

        $this->view->locations = $locations;
        $this->view->unit_id = $unitID;
        echo $this->view->render('PickupDropoff/listlocations.phtml');
        die();
    }

    public function mapAction(){

        // get the unit information
        ob_clean();
        $location_id = $this->_getParam('location_id');
        $model = new RM_PickupDropoff();
        $locationData = $model->getLocationData($location_id)->toArray();
        $locationData = $locationData[0];

        // construct the address for GMAPs to use
        $address = "";
        if ($locationData['address1']) $address .= $locationData['address1'];
        if ($locationData['address2']) $address .= ", ".$locationData['address2'];
        if ($locationData['address3']) $address .= ", ".$locationData['address3'];
        if ($locationData['city']) $address .= ", ".$locationData['city'];
        if ($locationData['address1']) $address .= ", ".$locationData['state'];
        if ($locationData['address1']) $address .= ", ".$locationData['postcode'];
        if ($locationData['address1']) $address .= ", ".$locationData['country'];
        $this->view->address = $address;

        // gmaps settings
        $settingsObj = new RM_GMaps();
        $settings = $settingsObj->getSettings()->toArray();
        $this->view->settings = $settings[0];

        // translation for back button
        $this->view->backTranslation = RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_( 'Common', 'BackStep');

        $this->view->unit_id = $this->_getParam('unit_id');

        // call the view
        echo $this->view->render('PickupDropoff/map.phtml');
        die();
    }

    public function selectlocationsJsonAction(){

        $unitID = $this->_getParam('unit_id');
        $type = $this->_getParam('select_type');
        $locationID = $this->_getParam('location_id');

        $criteria = RM_Reservation_Manager::getInstance()->getCriteria();

        // perform a check to make sure our criteria object is the correct instance
        // if it's not it will cause errors, so we check it and if it isn't we create
        // it as the correct object
        if (!$criteria instanceof RM_Unit_Search_Criteria) {
            $criteria = new RM_Unit_Search_Criteria($data);
        }

        if ($type=="pickup"){
            $criteria->pickup = $locationID;
        } else {
            $criteria->dropoff = $locationID;
        }

        RM_Reservation_Manager::getInstance()->setCriteria($criteria);

        return array(
        'data' => array(
            'success' => true,
            'pickup' => $criteria->pickup,
            'dropoff' => $criteria->dropoff
            )
        );
    }
}